package com.hgb.mall.portal.services.impl;

import com.github.pagehelper.PageHelper;
import com.hgb.mall.mbg.entity.*;
import com.hgb.mall.mbg.mapper.*;
import com.hgb.mall.portal.dao.HomeDao;
import com.hgb.mall.portal.domain.FlashPromotionProduct;
import com.hgb.mall.portal.domain.HomeContentResult;
import com.hgb.mall.portal.domain.HomeFlashPromotion;
import com.hgb.mall.portal.services.HomeService;
import com.hgb.mall.portal.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * 首页内容管理Service实现类
 * Created by hgb on 2020/1/28.
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private CmsSubjectMapper subjectMapper;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList(new Date()));
        //获取推荐品牌
        result.setBrandList(homeDao.getRecommendBrandList(0, 6));
        //获取秒杀信息
        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        result.setNewProductList(homeDao.getNewProductList(0, 3));
        //获取人气推荐
        result.setHotProductList(homeDao.getHotProductList(0, 3));
        //获取推荐专题
        result.setSubjectList(homeDao.getRecommendSubjectList(0, 3));
        result.setNote("来自本地服务器");
        return result;
    }

    @Override
    public List<PmsProduct> recommendProductList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return productMapper.select(c -> c
                .where(PmsProductDynamicSqlSupport.deleteStatus, isEqualTo(0))
                .and(PmsProductDynamicSqlSupport.publishStatus, isEqualTo(1))
        );
    }

    @Override
    public List<PmsProductCategory> getProductCateList(Long parentId) {

        return productCategoryMapper.select(c -> c
                .where(PmsProductCategoryDynamicSqlSupport.showStatus, isEqualTo(1))
                .and(PmsProductCategoryDynamicSqlSupport.parentId, isEqualTo(parentId))
                .orderBy(PmsProductCategoryDynamicSqlSupport.sort.descending())
        );
    }

    @Override
    public List<CmsSubject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return subjectMapper.select(c -> c
                .where(CmsSubjectDynamicSqlSupport.showStatus, isEqualTo(1))
                .and(CmsSubjectDynamicSqlSupport.categoryId, isEqualToWhenPresent(cateId))
        );
    }

    @Override
    public List<PmsProduct> hotProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeDao.getHotProductList(offset, pageSize);
    }

    @Override
    public List<PmsProduct> newProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeDao.getNewProductList(offset, pageSize);
    }

    @Override
    public HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
        Date now = new Date();
        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
        if (flashPromotion != null) {
            //获取当前秒杀场次
            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
            if (flashPromotionSession != null) {
                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
                if (nextSession != null) {
                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
                }
                //获取秒杀商品
                List<FlashPromotionProduct> flashProductList = homeDao
                        .getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
                homeFlashPromotion.setProductList(flashProductList);
            }
        }
        return homeFlashPromotion;
    }

    //获取下一个场次信息
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
        List<SmsFlashPromotionSession> promotionSessionList =
                promotionSessionMapper.select(c -> c
                        .where(SmsFlashPromotionSessionDynamicSqlSupport.startTime, isGreaterThan(date))
                        .orderBy(SmsFlashPromotionSessionDynamicSqlSupport.startTime)

                );
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }

    private List<SmsHomeAdvertise> getHomeAdvertiseList(Date date) {
        return advertiseMapper.select(c -> c
                .where(SmsHomeAdvertiseDynamicSqlSupport.status, isEqualTo(1))
                .and(SmsHomeAdvertiseDynamicSqlSupport.startTime, isLessThanOrEqualTo(date))
                .and(SmsHomeAdvertiseDynamicSqlSupport.endTime, isGreaterThanOrEqualTo(date))
                .orderBy(SmsHomeAdvertiseDynamicSqlSupport.sort.descending())
        );
    }

    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
        Date currDate = DateUtil.getDate(date);
        List<SmsFlashPromotion> flashPromotionList =
                flashPromotionMapper.select(c -> c
                        .where(SmsFlashPromotionDynamicSqlSupport.status, isEqualTo(1))
                        .and(SmsFlashPromotionDynamicSqlSupport.startDate, isLessThanOrEqualTo(currDate))
                        .and(SmsFlashPromotionDynamicSqlSupport.endDate, isGreaterThanOrEqualTo(currDate))
                );
        if (!CollectionUtils.isEmpty(flashPromotionList)) {
            return flashPromotionList.get(0);
        }
        return null;
    }

    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
        Date currTime = DateUtil.getTime(date);
        List<SmsFlashPromotionSession> promotionSessionList =
                promotionSessionMapper.select(c -> c
                        .where(SmsFlashPromotionSessionDynamicSqlSupport.startTime, isLessThanOrEqualTo(currTime))
                        .and(SmsFlashPromotionSessionDynamicSqlSupport.endTime, isGreaterThanOrEqualTo(currTime))
                );
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }
}
